#!/bin/bash

source /etc/profile

if [ $# -eq 1 ]; then
	targetHour=$1
else
	targetHour=`date -d "-1 hours" +"%Y%m%d%H"`
fi
path=$(cd $(dirname $0);pwd)
parentPath=$(dirname ${path})
targetDate=${targetHour:0:8}
if [ ! -d "${path}/${targetDate}/conversion" ];then
	mkdir -p ${path}/${targetDate}/conversion
fi
day=$(date -d"${targetDate}" "+%Y-%m-%d")


source ${parentPath}/configs/mysql_config


path_prefix="${path}/${targetDate}/conversion/${targetHour}"
conversion_config_path_prefix="${parentPath}/configs/"

#页面转化
#nas_conversion_page
python ${path}/ReadfromMysql.py ${host} ${port} ${user} ${password} ${db} "cpid,cid,sid,pages" "nas_conversion_page" > ${path_prefix}_page.txt
if [ -s ${path_prefix}_page.txt ];then
    hive -e "set mapred.job.name=[NAS][conversion][page][${targetHour}];use formatlog;add file ${path_prefix}_page.txt;add file ${path}/filter_page.py;insert overwrite local directory '${path_prefix}_reslt_page' select b.cid,b.sid,${targetDate},b.aid,b.pagetarget,count(1),count(distinct b.nasid),'1' from (select transform(a.cid,a.sid,a.mnasid,a.eps) using 'python filter_page.py ${targetHour}_page.txt' as (cid,sid,aid,pagetarget,nasid) from (select cid,sid,session,max(nasid) as mnasid, concat_ws(',',collect_set(ep)) as eps from nas_tracking_format_hour where day=${targetDate} and cid is not null and sid is not null group by cid,sid,session) a) b group by b.cid,b.sid,b.aid,b.pagetarget;"
fi

#事件转化
#nas_conversion_event
python ${path}/ReadfromMysql.py ${host} ${port} ${user} ${password} ${db} "ceid,cid,sid,eid,times" "nas_conversion_event" > ${path_prefix}_event.txt
if [ -s ${path_prefix}_event.txt ];then
# not sum(b.cvnum)  直接统计行数 count(1)
    hive -e "set mapred.job.name=[NAS][conversion][event][${targetHour}];use formatlog;add file ${path_prefix}_event.txt;add file ${path}/event_deal.py;insert overwrite local directory '${path_prefix}_reslt_event' select b.cid,b.sid,${targetDate},b.ceid,b.eventid,count(1),count(distinct b.nasid) as uv,'2' from (select transform(a.cid,a.sid,a.session,a.mnasid,a.eventids) using 'python event_deal.py ${targetHour}_event.txt' as (cid,sid,nasid, ceid, eventid ,cvnum) from (select cid,sid,session,max(nasid) as mnasid,concat_ws(',',collect_list(cast(eventid as string))) as eventids from nas_tracking_format_hour where day=${targetDate} and cid is not null and sid is not null group by cid,sid,session) a ) b group by b.cid,b.sid,b.ceid,b.eventid;"
fi

#时长转化
#nas_conversion_time
python ${path}/ReadfromMysql.py ${host} ${port} ${user} ${password} ${db} "cid,sid,ctid,time" "nas_conversion_time" > ${path_prefix}_time.txt
if [ -s ${path_prefix}_time.txt ];then
    hive -e "set mapred.job.name=[NAS][conversion][time][${targetHour}];use formatlog;add file ${path}/time_deal.py; add file ${path_prefix}_time.txt;insert overwrite local directory '${path_prefix}_reslt_time' select b.cid,b.sid,${targetDate},b.ctid,b.dtime,count(1),count(distinct b.nasid),'3' from (select transform(a.cid,a.sid,a.session,a.mnasid,a.dt) using 'python time_deal.py ${targetHour}_time.txt' as (cid,sid,nasid,ctid,dtime,num) from (select cid,sid,session,max(nasid) as mnasid,((max(time)-min(time))/1000.0 + 60) as dt from nas_tracking_format_hour where day=${targetDate} and cid is not null and sid is not null group by cid,sid,session) a ) b group by b.cid,b.sid,b.ctid,b.dtime;"
fi

#页数转化
#nas_conversion_pagenum
python ${path}/ReadfromMysql.py ${host} ${port} ${user} ${password} ${db} "cid,sid,cpnid,pagenum" "nas_conversion_pagenum" > ${path_prefix}_pagenum.txt
if [ -s ${path_prefix}_pagenum.txt ];then
    hive -e "set mapred.job.name=[NAS][conversion][pagenum][${targetHour}];use formatlog;add file ${path}/pagenum_deal.py; add file ${path_prefix}_pagenum.txt;insert overwrite local directory '${path_prefix}_reslt_pageNum'  select b.cid,b.sid,${targetDate},b.cpnid,b.pagenum,count(1),count(distinct b.nasid),'4' from (select transform(a.cid,a.sid,a.session,a.mnasid,a.dt) using 'python pagenum_deal.py ${targetHour}_pagenum.txt' as (cid,sid,nasid,cpnid,pagenum,num) from (select cid,sid,session,max(nasid) as mnasid,count(1) as dt from nas_tracking_format_hour where day=${targetDate} and cid is not null and sid is not null group by cid,sid,session) a ) b group by b.cid,b.sid,b.cpnid,b.pagenum;"
fi
wait

cat /dev/null > ${path_prefix}_allresult.txt
cat ${path_prefix}_reslt_page/* >> ${path_prefix}_allresult.txt
cat ${path_prefix}_reslt_event/* >> ${path_prefix}_allresult.txt
cat ${path_prefix}_reslt_time/* >> ${path_prefix}_allresult.txt
cat ${path_prefix}_reslt_pageNum/* >> ${path_prefix}_allresult.txt


python ${path}/load_conversion_to_mysql.py ${path_prefix}_allresult.txt ${host} ${port} ${user} ${password} ${db} ${targetDate}
